import HTTP from './Http';
import GameControl from './GameControl';
import { StorageName } from './StorageName';

// 访问方式：
// 例如登陆接口：Constants.API.login().then((res) => { if (res) {}})

export default class API {
    _TAG: any = "API ";
    _versions: string = "v1.0";
    private _url = GameControl.getInstance().getHttpURLArr()[0];
    private _platformCode = GameControl.getInstance().getHttpURLArr()[1];
    private _httpURL = this._url + '/' + this._platformCode;

    // 单例模式
    private static _instance: API = null;
    public static getInstance() {
        if (this._instance == null) {
            this._instance = new API();
        }
        return this._instance;
    }

    private _http: HTTP = null;
    constructor() {
        this._http = HTTP.getInstance();
    }

    post(url, msgData) {
        return new Promise<any>((resolve, reject) => {
            // WEB平台
            if (GameControl.getInstance().getPlatform() === 'h5') {
                resolve({ status: false, data: 'h5' });
            } else {
                // 其他平台
                printLog('向后端发起请求,请求地址:', url);
                this._http.post(url, msgData, (data) => {
                    if (data.code == 200) {
                        resolve({
                            status: true,
                            data: data.msg
                        })
                    } else {
                        resolve({
                            status: false,
                            data: data.msg
                        })
                    }
                })
            }
        })
    }

    // 登录
    login(msgData) {
        return this.post(this._httpURL + '/login', msgData);
    }

    // 保存用户信息
    saveUserInfo(msgData) {
        return this.post(this._httpURL + '/saveUserInfo', msgData);
    }

    // 获取用户信息
    getUserInfo(msgData) {
        return this.post(this._httpURL + '/getUserInfo', msgData);
    }

    // 更新用户数据
    userUpdata() {
        let data = GameControl.getInstance().getLocalStorage(StorageName.GAME_DATA);
        if (data) {
            return this.post(this._httpURL + '/userUpdata', data);
        } else {
            printLog('提交失败');
        }
    }

    // 获取广告信息
    getIconInfo() {
        return this.post(this._httpURL + '/getIconInfo', {
            open_id: GameControl.getInstance().getOpenID()
        })
    }

    // icon打点统计
    iconClick(msgData) {
        return this.post(this._httpURL + '/iconClick', msgData);
    }

    // 重置用户数据
    reset() {
        return this.post(this._httpURL + '/getIconInfo', {
            open_id: GameControl.getInstance().getOpenID()
        })
    }

    // 签到
    signView() {
        return this.post(this._httpURL + '/signView', {
            open_id: GameControl.getInstance().getOpenID()
        })
    }

    // 录屏分享数
    shareScreen() {
        return this.post(this._httpURL + '/shareScreen', {
            open_id: GameControl.getInstance().getOpenID()
        })
    }

    // 获取配置文件
    getJsonConfig() {
        return this.post(this._url + '/common/getJsonConfig', {
            app_id: GameControl.getInstance().getAppID()
        })
    }

    // 获取游戏橱窗点击允许的游戏的玩家数量
    getAllow(app_id) {
        return this.post(this._url + '/common/getAllow', {
            open_id: GameControl.getInstance().getOpenID(),
            app_id: GameControl.getInstance().getAppID(),
            other_app_id: app_id
        })
    }
}
